from selenium.webdriver import ActionChains

from .multiple import run_multiple_operation
from utils.selenium.perform import WPerform


class MouseOptionType:
    """
    鼠标操作类型
    """
    click = 1
    right_click = 2
    double_click = 3
    hover = 4


def double_click(perform: WPerform, element, *args):
    """双击"""
    action_chains = ActionChains(perform.driver)
    action_chains.double_click(element).perform()


def context_click(perform: WPerform, element, *args):
    """右击"""
    action_chains = ActionChains(perform.driver)
    action_chains.context_click(element).perform()


def move_to_element(perform: WPerform, element, *args):
    """右击"""
    action_chains = ActionChains(perform.driver)
    action_chains.move_to_element(element).perform()


def run_mouse_operation(perform: WPerform, item, other_query):
    """
    执行鼠标操作
    :param driver: 页面的 driver
    :param item: 用例数据
    :param other_query: 顶层带过来的额外参数
    :return:
    """
    mouse_operation_type = item['mouse_operation_type']
    if mouse_operation_type == MouseOptionType.click:
        run_multiple_operation(perform, item, 'click', (), other_query)
    elif mouse_operation_type == MouseOptionType.double_click:
        run_multiple_operation(perform, item, double_click, (), other_query)
    elif mouse_operation_type == MouseOptionType.right_click:
        run_multiple_operation(perform, item, context_click, (), other_query)
    elif mouse_operation_type == MouseOptionType.hover:
        run_multiple_operation(perform, item, move_to_element, (), other_query)